package com.hui.servlet;

import com.hui.dao.ProductDao;
import com.hui.entity.*;
import com.hui.service.CategoryService;
import com.hui.service.ProductService;
import com.hui.service.RecodeService;
import com.hui.service.WareHouseService;
import com.hui.vo.ManagerVo;
import com.hui.vo.ProductVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @date 2023/2/11 9:46
 */
@WebServlet(urlPatterns = {"/getProductAll", "/addProduct", "/toAddProductPage", "/toProductPage", "/delProduct", "/toUpdateProductPage", "/updateProduct", "/updateProductNum"})
public class ProductServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();

        if (uri.endsWith("getProductAll")) {
            getProductAll(req, resp);
        } else if (uri.endsWith("addProduct")) {
            addProduct(req, resp);
        } else if (uri.endsWith("toAddProductPage")) {
            toAddProductPage(req, resp);
        } else if (uri.endsWith("delProduct")) {
            delProduct(req, resp);
        } else if (uri.endsWith("toUpdateProductPage")) {
            toUpdateProductPage(req, resp);
        } else if (uri.endsWith("updateProduct")) {
            updateProduct(req, resp);
        } else if (uri.endsWith("updateProductNum")) {
            updateProductNum(req, resp);
        } else if (uri.endsWith("toProductPage")) {
            toProductPage(req, resp);
        }
    }

    /**
     * 出库
     **/
    private void updateProductNum(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        int num = Integer.parseInt(req.getParameter("productNum"));
        int productId = Integer.parseInt(req.getParameter("productId"));
        ProductService service = new ProductService();
        service.updateProductNumById(num, productId);//修改库存数量

        HttpSession session = req.getSession();
        //获取登陆的用户对象---保存在session的对象
        Manager mgr = (Manager) session.getAttribute("currentUser");
        int mgrId = mgr.getMgrId();

        Recode recode = new Recode(null, productId, mgrId, new Date(), num);


        RecodeService recodeService = new RecodeService();
        recodeService.addRecode(recode);//添加到出库记录

        resp.sendRedirect("getProductAll");


    }

    /**
     * 跳转到出库界面
     **/
    private void toProductPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int id = Integer.parseInt(req.getParameter("id"));

        ProductService service = new ProductService();
        ProductVo productVo = service.findProductById(id);

        //获取分类数据
        CategoryService service1 = new CategoryService();
        List<Category> categoryList = service1.findCategoryAll();
        //获取参数数据
        WareHouseService wareHouseService = new WareHouseService();
        List<WareHouse> wareHouseList = wareHouseService.getWareHouseAll();

        req.setAttribute("categoryList", categoryList);
        req.setAttribute("wareHouseList", wareHouseList);

        req.setAttribute("productVo", productVo);
        req.getRequestDispatcher("productOutbound.jsp").forward(req, resp);

    }


    /**
     * 修改数据
     **/
    private void updateProduct(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("productId"));
        String productName = req.getParameter("productName");
        String productDesc = req.getParameter("productDesc");
        int productNum = Integer.parseInt(req.getParameter("productNum"));
        int warehouseId = Integer.parseInt(req.getParameter("warehouseId"));
        int categoryId = Integer.parseInt(req.getParameter("categoryId"));

        Product product = new Product(id, productName, categoryId, productNum, productDesc, new Date(), "1", warehouseId);

        ProductService service = new ProductService();
        service.updateProductById(product);
        resp.sendRedirect("getProductAll");
    }


    /**
     * 跳转到修改界面
     **/
    private void toUpdateProductPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int id = Integer.parseInt(req.getParameter("id"));
        ProductService service = new ProductService();
        ProductVo productVo = service.findProductById(id);

        //获取分类数据
        CategoryService service1 = new CategoryService();
        List<Category> categoryList = service1.findCategoryAll();
        //获取参数数据
        WareHouseService wareHouseService = new WareHouseService();
        List<WareHouse> wareHouseList = wareHouseService.getWareHouseAll();

        req.setAttribute("categoryList", categoryList);
        req.setAttribute("wareHouseList", wareHouseList);

        req.setAttribute("productVo", productVo);
        req.getRequestDispatcher("products_update.jsp").forward(req, resp);

    }

    /**
     * 删除数据
     **/
    private void delProduct(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));

        ProductService service = new ProductService();
        service.delProduct(id);
    }

    /**
     * 添加货物，并显示在前台界面
     **/
    private void addProduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String productName = req.getParameter("productName");
        String productDesc = req.getParameter("productDesc");
        int productNum = Integer.parseInt(req.getParameter("productNum"));
        int warehouseId = Integer.parseInt(req.getParameter("warehouseId"));
        int categoryId = Integer.parseInt(req.getParameter("categoryId"));

        Product product = new Product(null, productName, categoryId, productNum, productDesc, new Date(), "1", warehouseId);

        ProductService service = new ProductService();
        service.addProduct(product);
        resp.sendRedirect("getProductAll");
    }

    /**
     * 跳转到新增界面，并把分类和仓库数据展示在该界面上
     **/
    private void toAddProductPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取分类数据
        CategoryService service = new CategoryService();
        List<Category> categoryList = service.findCategoryAll();
        //获取参数数据
        WareHouseService wareHouseService = new WareHouseService();
        List<WareHouse> wareHouseList = wareHouseService.getWareHouseAll();

        req.setAttribute("categoryList", categoryList);
        req.setAttribute("wareHouseList", wareHouseList);
        req.getRequestDispatcher("products_add.jsp").forward(req, resp);

    }

    /**
     * 查询所有货物，并显示在前台界面
     **/
    private void getProductAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ProductService service = new ProductService();
        List<ProductVo> list = service.getProductAll();
        //将返回的list集合数据转发给前端界面
        req.setAttribute("list", list);
        req.getRequestDispatcher("products_list.jsp").forward(req, resp);

    }
}
